/**
 * 
 */
package hihocoder;

import java.util.Scanner;

/**
 * @author zhongfang
 *
 */
public class 两个机器人 {
	static int[][][][] result;

	private static int getTarget(int p1i, int p1j, int p2i, int p2j, int matrix[][]) {
		if (result[p1i][p1j][p2i][p2j] != -1) {
			System.out.println("已经计算过");
			return result[p1i][p1j][p2i][p2j];
		}
		int n = matrix.length;
		int m = matrix[0].length;
		if (p1i == n - 1 && p1j == m - 1 && p2i == 0 && p2j == 0) {// 两个点到达目的
			return 0;
		}
		int res = -1;
		// A向右，B向左
		int newA = p1i;
		if (p1i != n - 1 && matrix[p1i + 1][p1j] != 1) {// A能动
			newA = p1i + 1;
		}
		int newB = p2i;
		if (p2i != 0 && matrix[p2i - 1][p2j] != 1) {// B向左没障碍
			newB = p2i - 1;
		}
		if (newA == p1i && newB == p2i) {
			// 两个不能动，没必要递归
		} else {
			if ((newA == newB && p1j == p2j) || (newA == p2i && newB == p1i && p1j == p2j)) {// 不能相碰

			} else {
				res = getTarget(newA, p1j, newB, p2j, matrix);
				if (res != -1) {
					res++;
				}
			}
		}

		int newAj = p1j;
		if (p1j != m - 1 && matrix[p1i][p1j + 1] != 1) {
			newAj = p1j + 1;
		}
		int newBj = p2j;
		if (p2j != 0 && matrix[p2i][p2j - 1] != 1) {
			newBj = p2j - 1;
		}
		if (newBj == p2j && newAj == p1j) {
			//
		} else {
			if ((newAj == newBj && p1i == p2i) || (newAj == p2j && newBj == p1j && p1i == p2i)) {// 不能相碰

			} else {
				int tmp = getTarget(p1i, newAj, p2i, newBj, matrix);
				if (tmp != -1) {
					tmp++;
				}

				if (res != -1) {
					if (tmp != -1) {
						res = Math.min(tmp, res);
					} // 否则，不变
				} else {// res为-1更新为tmp
					res = tmp;
				}
			}
		}
		return res;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int m = scanner.nextInt();
		int[][] matrix = new int[n][m];
		scanner.nextLine();
		for (int i = 0; i < matrix.length; i++) {
			String string = scanner.nextLine();
			for (int j = 0; j < m; j++) {
				matrix[i][j] = string.charAt(j) - '0';
			}
		}
		// int nonReach = Integer.MAX_VALUE;
		result = new int[n][m][n][m];
		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < m; j++) {
				for (int j2 = 0; j2 < n; j2++) {
					for (int k = 0; k < m; k++) {
						result[i][j][j2][k] = -1;
					}
				}
			}
		}
		System.out.println(getTarget(0, 0, n - 1, m - 1, matrix));// 40超时
		// int[][][][] dp = new int[n][m][n][m];
		// for (int i = 0; i < dp.length; i++) {
		// for (int j = 0; j < m; j++) {
		// for (int j2 = 0; j2 < n; j2++) {
		// for (int k = 0; k < m; k++) {
		// dp[i][j][m][n] = nonReach;
		// }
		// }
		// }
		// }
		// dp[0][0][n - 1][m - 1] = 1;
		// for (int i1 = 0; i1 < n; i1++) {
		// for (int j1 = 0; j1 < m; j1++) {
		// for (int i2 = n - 1; i2 >= 0; i2--) {
		// for (int j2 = m - 1; j2 >= 0; j2--) {
		// if (i1 == 0 && j1 == 0 && i2 == n - 1 && j2 == m - 1) {
		// continue;
		// }
		// if (i1 == i2 && j1 == j2) {// 不能处在相同位置
		// continue;
		// }
		// if (i1 == i2 && j1 == j2 - 1) {
		// continue;
		// }
		// if (j1 == j2 && i1 == i2 - 1) {
		// continue;
		// }
		// if (matrix[i1][j1] != 1 && matrix[i2][j2] != 1) {
		// if (dp[i1 - 1][j1][m + 1][n] != nonReach) {// 上一个状态是可达的
		// dp[i1][j1][i2][j2] = dp[i1 - 1][j1][m + 1][n] + 1;
		// }
		// } else if (matrix[i1][j1] == 1) {
		// if (dp[i1][j1][i2 + 1][j2] != nonReach) {
		// dp[i1][j1][i2][j2] = dp[i1][j1][i2 + 1][j2] + 1;
		// }
		// } else if (matrix[i2][j2] == 1) {
		//
		// }
		// }
		// }
		// }
		// }
	}
}
